/*******************************************************************************
 * Copyright (c) 2008 Bartosz Michalik and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bartosz Michalik (bartosz.michalik@gmail.com)
 *******************************************************************************/

package org.eclipse.pde.internal.core.product;

import java.io.PrintWriter;
import org.eclipse.pde.internal.core.iproduct.*;
import org.w3c.dom.Node;

/**
 * Transformation interface adapter.
 * @author Bartosz Michalik
 *
 */
public abstract class ProductTransform extends ProductObject implements IProductTransform {

	/**
	 * Parse additional data
	 * @param node
	 */
	abstract protected void parseData(Node node);

	abstract public void write(String indent, PrintWriter writer);

	abstract public void writeTransformEntry(String indent, PrintWriter writer, String type);

	public int getMode() {
		return MODE_ONE;
	}

	protected Object fTransformed;
	protected String fXPath;

	public ProductTransform(IProductModel model) {
		super(model);
		// TODO Auto-generated constructor stub
	}

	private static final long serialVersionUID = 358185896134061896L;

	public String[] getTransformTypes() {
		return new String[] {IProductCustomizationConstants.TYPE_XSLT};
	}

	public void setTransformedObject(Object transformedObject) {
		fTransformed = transformedObject;
		fXPath = ProductTransformFactory.toXPath(fTransformed, true);
	}

	public Object getTransformedObject() {
		return fTransformed;
	}

	public String getXPath() {
		return fXPath;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return fXPath.hashCode();
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		//TODO research
		if (obj.getClass().equals(this.getClass())) {
			IProductTransform pt = (IProductTransform) obj;
			return fXPath.equals(pt.getXPath());
		}
		return false;
	}

	public void parse(Node node) {
		Node targetAttribute = node.getAttributes().getNamedItem(TARGET_ATT);
		fTransformed = ProductTransformFactory.fromXPath(targetAttribute.getNodeValue());
		fXPath = targetAttribute.getNodeValue();
		if (fTransformed == null)
			throw new IllegalStateException("There is no object for: " + fXPath); //$NON-NLS-1$
		parseData(node);
	}
}
